package com.cloudwise.thread;

import lombok.SneakyThrows;

import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author ：Teacher陈
 * @date ：Created in 2021/1/6 16:34
 * @description：测试线程池
 * @modified By：
 * @version: 1
 */
public class ThreadPool {

    @SneakyThrows
    public static void main(String[] args) {
        /*ExecutorService pool= Executors.newCachedThreadPool();

        for (int i = 0; i < 100; i++) {
            Thread.sleep(1000);
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
        pool.shutdown();*/


        ScheduledExecutorService pool=Executors.newScheduledThreadPool(2);
        //10:10秒之后执行该线程，执行间隔5秒，TimeUnit.SECONDS秒
        pool.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName()+new Date());
            }
        },10,5, TimeUnit.SECONDS);
        //关闭线程池
        pool.shutdown();
    }
}
